import React from 'react';
import {connect} from 'react-redux';
import TopNav from './../components/nav/TopNav';
import {login, logout, loginPrompt, loginCancel,register,switchRegister,switchLogin,swtichSearch} from '../actions/authentication';
import LoginForm from '../components/auth/LoginForm';
import MuiThemeProvider from 'material-ui/styles/MuiThemeProvider';
import Search from '../components/search/Search';
import Storage from '../utils/storage';

class Main extends React.Component {
    constructor(props) {
        super(props);
    }

    componentDidMount() {
        let profile = Storage.getJSON(this.props.PROFILE_STORAGE_KEY);
        if (profile) {
            this.context.router.push('search');
        } else {
            this.context.router.push('login');
        }
    }

    render() {
        return (
            <div className='container'>
                <TopNav {...this.props}/>

                <div className="jumbotron">
                    <div className="text-center">
                        {this.props.children}
                    </div>
                </div>

            </div>
        );
    }
}

const mapDispatchToProps = (dispatch) => {
    return {
        logout: () => {
            dispatch(logout());
        }
    }
};

const mapStateToProps = (state) => {
    return {
        applicationName: state.global.applicationName,
        environmentName: state.global.environmentName,
        ...state.authentication
    };
};

Main.contextTypes = {
    router: React.PropTypes.func.isRequired
}

export default connect(mapStateToProps, mapDispatchToProps)(Main);
